package com.lyfx.ly.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyfx.ly.entity.ClassCourseEntity;
import com.lyfx.ly.vo.CourseVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lyfx.ly.entity.ClassCourseCategoryEntity;
import com.lyfx.ly.service.ClassCourseCategoryService;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.R;



/**
 * **课程表和课程分类的关联表
 *
 * @author lsy
 * @email lsy@gmail.com
 * @date 2020-07-05 18:03:59
 */
@RestController
@RequestMapping("ly/classcoursecategory")
public class ClassCourseCategoryController {
    @Autowired
    private ClassCourseCategoryService classCourseCategoryService;


    /**
     * 获取三级分类下的所有课程
     * ly/classcoursecategory/courses/list
     */
    @GetMapping("/courses/list")
    public R coursesList(@RequestParam(value = "catId",required = true)Long catId){
        List<ClassCourseEntity> vos =classCourseCategoryService.getCoursesByCatId(catId);
        List<CourseVo> collect = vos.stream().map(item -> {
            CourseVo courseVo = new CourseVo();
            courseVo.setCourseId(item.getCourseId());
            courseVo.setCourseName(item.getName());
            courseVo.setCourseDescript(item.getDescript());
            return courseVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ly:classcoursecategory:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = classCourseCategoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    @GetMapping("/list/info")
    public R listInfo(@RequestParam Map<String, Object> params){
        PageUtils page = classCourseCategoryService.queryPageInfo(params);
        return R.ok().put("page", page);
    }
    /**
     * 获取当前课程关联列表
     */
    @GetMapping("/category/list")
    @RequiresPermissions("ly:classcoursecategory:list")
    public R categoryList(@RequestParam("courseId") Long courseId){
        List<ClassCourseCategoryEntity> data = classCourseCategoryService.list(
                new QueryWrapper<ClassCourseCategoryEntity>().eq("course_id", courseId));

        return R.ok().put("data", data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ly:classcoursecategory:info")
    public R info(@PathVariable("id") Long id){
		ClassCourseCategoryEntity classCourseCategory = classCourseCategoryService.getById(id);

        return R.ok().put("classCourseCategory", classCourseCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ly:classcoursecategory:save")
    public R save(@RequestBody ClassCourseCategoryEntity classCourseCategory){
		classCourseCategoryService.saveDetail(classCourseCategory);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ly:classcoursecategory:update")
    public R update(@RequestBody ClassCourseCategoryEntity classCourseCategory){
		classCourseCategoryService.updateById(classCourseCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ly:classcoursecategory:delete")
    public R delete(@RequestBody Long[] ids){
		classCourseCategoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
